﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.Tool.UserControls;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.Tool.Forms
{
    public partial class frm_ImportEdit : DevExpress.XtraEditors.XtraForm
    {
        public int Id;
        public DateTime Date;
        public string ToolId;
        public decimal PriceJpy;
        public string MakerId;
        public string SupplierId;
        public int Quantity;
        public string Note;

        private DataTable dt = new DataTable();
        private ucImport parent;

        public frm_ImportEdit(ucImport fImport)
        {
            InitializeComponent();
            btUpdate.Image = ERP_RORZE_TOOL.Properties.Resources.accept;
            btTCancel.Image = ERP_RORZE_TOOL.Properties.Resources.cancel;
            parent = fImport;
            this.CancelButton = this.btTCancel;
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            UpdateTool();
        }

        private void btTCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void XtraForm1_Load(object sender, EventArgs e)
        {
            BindDataTools();
            BindDataToMaker();
            BindDataSupplier();
            dtpTDate.Value = Date;
            txtTPrice.Text = PriceJpy.ToString();
            txtTQuantity.Text = Quantity.ToString();
            memoTNote.Text = Note;

        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }
        #region Private methods

        private bool GetDataTool(Import o)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(dtpTDate.Value.ToString()))
            {
                MessageBox.Show(ToolConstants.Message.Import.DateCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbToolID.SelectedValue) || cmbToolID.SelectedItem == cmbToolID.Items[0])
            {
                MessageBox.Show(ToolConstants.Message.Import.ToolIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbMakerID.SelectedValue) || cmbMakerID.SelectedItem == cmbMakerID.Items[0])
            {
                MessageBox.Show(ToolConstants.Message.Import.MakerCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtTPrice.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Import.PriceCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (decimal.Parse(txtTPrice.Text) <= 0)
            {
                MessageBox.Show(ToolConstants.Message.Import.PriceMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTPrice.Focus();
                return false;

            }

            if (Utilities.IsNullOrEmpty(cmbTSupplierID.SelectedValue) || cmbTSupplierID.SelectedItem == cmbTSupplierID.Items[0])
            {
                MessageBox.Show(ToolConstants.Message.Import.SupplierCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtTQuantity.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Import.QuantityCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (int.Parse(txtTQuantity.Text) <= 0)
            {
                MessageBox.Show(ToolConstants.Message.Import.QuantityMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTQuantity.Focus();
                return false;

            }

            #endregion

            o.ID = Id;
            o.Date = dtpTDate.Value;
            o.ToolID = cmbToolID.SelectedValue.ToString();
            o.MakerID = cmbMakerID.SelectedValue.ToString();
            o.PriceJPY = decimal.Parse(txtTPrice.Text);
            o.SupplierID = cmbTSupplierID.SelectedValue.ToString();
            o.Quantity = int.Parse(txtTQuantity.Text);
            o.Note = memoTNote.Text;

            return true;
        }

        private void BindDataTools()
        {
            dt.Clear();
            ToolHelper helper = new ToolHelper(new Tools());
            dt = helper.GetAllByToolType();
            DataRow row = dt.NewRow();

            row[ToolConstants.Entities.Tool.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Tool.FieldName.ToolID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbToolID.DataSource = dt;
            cmbToolID.DisplayMember = ToolConstants.Entities.Tool.FieldName.FullName;
            cmbToolID.ValueMember = ToolConstants.Entities.Tool.FieldName.ToolID;

            cmbToolID.SelectedValue = ToolId;
            if (cmbToolID.SelectedItem == null)
            {
                cmbToolID.SelectedItem = cmbToolID.Items[0];
            }
        }

        private void BindDataSupplier()
        {
            SupplierHelper helper = new SupplierHelper(new Supplier());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            cmbTSupplierID.DataSource = dt;
            cmbTSupplierID.DisplayMember = ToolConstants.Entities.Supplier.FieldName.FullName;
            cmbTSupplierID.ValueMember = ToolConstants.Entities.Supplier.FieldName.SupplierID;

            row[ToolConstants.Entities.Supplier.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Supplier.FieldName.SupplierID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbTSupplierID.SelectedValue = SupplierId;
            if (cmbTSupplierID.SelectedItem == null)
            {
                cmbTSupplierID.SelectedItem = cmbTSupplierID.Items[0];
            }
        }

        private void UpdateTool()
        {
            ImportHelper helper = new ImportHelper(new Import());
            if (GetDataTool(helper.ValueObject))
            {


                if (helper.Update() > 0)
                {

                    if (!Utilities.MessageConfirmationYesNo(ToolConstants.Message.UpdateSuccessAndQuestion))
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(ToolConstants.Message.UpdateFailed);
                }
                parent.BindDatatoGrid();

            }
        }

        private void BindDataToMaker()
        {
            MakerHelper helper = new MakerHelper(new Maker());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            cmbMakerID.DataSource = dt;
            cmbMakerID.DisplayMember = ToolConstants.Entities.Maker.FieldName.FullName;
            cmbMakerID.ValueMember = ToolConstants.Entities.Maker.FieldName.MakerID;
            row[ToolConstants.Entities.Maker.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Maker.FieldName.MakerID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbMakerID.SelectedValue = MakerId;
            if (cmbMakerID.SelectedItem == null)
            {
                cmbMakerID.SelectedItem = cmbMakerID.Items[0];
            }

        }


        #endregion


    }
}